<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
class Party extends Model
{
    // 活动未开始
    const NOT_START = 1;
    // 活动进行中
    const IS_HOLDING = 2;
    // 活动已结束
    const ALREADY_STOP = 3;

    // 状态正常
    const ONLINE = 1;
    // 被隐藏
    const OFFLINE = 0;

    // 咖啡馆status文本
    static protected $progressArray = [
        self::NOT_START => '未开始',
        self::IS_HOLDING  => '进行中',
        self::ALREADY_STOP => '已结束'
    ];

    // 表名称
    protected $table = 'party';
    // 开启自动时间戳
    public $timestamps = true;
    // 日期都存储为时间戳
    protected $dateFormat = 'U';
	//指定id
	protected $primaryKey ='id';
	// 新增字段
	protected $appends = ['start_datetime', 'end_datetime', 'progress', 'progress_text'];

	public function beans()
	{
		return $this->belongsToMany('App\Supplys', 'party_supply', 'party_id', 'supply_id');
	}

	public function  users(){
		return $this->belongsToMany('App\Users', 'party_user', 'party_id', 'user_id');
	}

	public function  sales(){
		return $this->hasMany('App\Sales','party_id');
	}

	public function cafe(){
		return $this->hasOne('App\Cafe','id','cafe_id');
	}
	public function partyPhoto(){
        return $this->hasMany('App\PartyPhoto','party_id','id');

    }

    public function partySignUp()
    {
        return $this->belongsToMany('App\Users', 'party_sign_up', 'party_id', 'user_id');
    }

    // 拼接开始日期时间字段
    public function getStartDatetimeAttribute()
    {
        return $this->attributes['start_date'].' '.$this->attributes['start_time'];
    }
    // 拼接结束日期时间字段
    public function getEndDatetimeAttribute()
    {
        return $this->attributes['end_date'].' '.$this->attributes['end_time'];
    }
    // 新增活动进展字段状态字段
    public function getProgressAttribute()
    {
        if (((strtotime($this->attributes['start_date'].' '.$this->attributes['start_time']) <= time()) && strtotime($this->attributes['end_date'].' '.$this->attributes['end_time']) >= time())){
            return self::IS_HOLDING;
        }elseif (strtotime($this->attributes['start_date'].' '.$this->attributes['start_time']) < time()) {
            return self::ALREADY_STOP;
        }else {
            return self::NOT_START;
        }
    }
    // 新增获取活动进展字段状态文字类型字段
    public function getProgressTextAttribute()
    {
        if (((strtotime($this->attributes['start_date'].' '.$this->attributes['start_time']) <= time()) && strtotime($this->attributes['end_date'].' '.$this->attributes['end_time']) >= time())){
            return self::$progressArray[self::IS_HOLDING];
        }elseif (strtotime($this->attributes['start_date'].' '.$this->attributes['start_time']) < time()) {
            return self::$progressArray[self::ALREADY_STOP];
        }else {
            return self::$progressArray[self::NOT_START];
        }
    }
}
